﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DemoObject;
using BusinessObjects;

public partial class Admin_VideoList : System.Web.UI.UserControl
{
    private const string ASCENDING = " ASC";
    private const string DESCENDING = " DESC";
    private const int PageSize = 20;

    private string GridViewSortDirection
    {

        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = "ASC";
            return ViewState["sortDirection"].ToString();
        }
        set { ViewState["sortDirection"] = value; }

    }
    public int PageIndex
    {
        get
        {
            if (Request["page"] != null)
                return int.Parse(Request["page"].ToString());
            return 1;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDLL();
            BindData("CreatedDate DESC");
        }

    }
    private void LoadDLL()
    {

        HashSet<string> hs = new HashSet<string>();
        hs.Add("Name ASC");
        ddlVideoCategory.DataSource = TblVideoCategoriesRepository.CreateInstant().SelectListItem(null, "", "", true, hs);
        ddlVideoCategory.DataTextField = "Name";
        ddlVideoCategory.DataValueField = "Id";
        ddlVideoCategory.DataBind();
    }
    private void BindData(string strSort)
    {
        HashSet<string> hs = new HashSet<string>();
        hs.Add(strSort);
        Guid? CategoryId = null;
        if (Request["VideoCategoryId"] != null && !Request["VideoCategoryId"].Equals(string.Empty))
            CategoryId = new Guid(Request["VideoCategoryId"].ToString());
        ddlVideoCategory.SelectedValue = CategoryId.HasValue ? CategoryId.Value.ToString() : "";
        string KeyWord = "";
        if (Request["KeyWord"] != null)
            KeyWord = Request["KeyWord"];
        txtTitle.Text = KeyWord;
        bool? IsVisible = GetIsVisibleBool();
        switch (IsVisible)
        {
            case true:
                cbIsVisible.Checked = true;
                cbIsVisible1.Checked = false;
                break;
            case false:
                cbIsVisible.Checked = false;
                cbIsVisible1.Checked = true;
                break;
            default:
                cbIsVisible.Checked = true;
                cbIsVisible1.Checked = true;
                break;
        }
        int RowCount = 0;
        IList<TblVideo> ls = TblVideoRepository.CreateInstant().SelectListItem(null, CategoryId, null, "", KeyWord, "", null, IsVisible, null, PageIndex, PageSize, out RowCount, hs);
        grv.DataSource = ls;
        Response.Write(RowCount.ToString());
        Response.Write("ls : " + ls.Count);
        Response.Write("PageIndex : " + PageIndex);
        Response.Write("PageSize : " + PageSize);
        grv.DataBind();
        smartPager.PageSize = PageSize;
        smartPager.QueryStringKey = "page";
        smartPager.RowCount = RowCount;
        smartPager.UrlFormatString = "/Admin/Module/Video/VideoList.aspx?VideoCategoryId=" + ddlVideoCategory.SelectedValue + "&KeyWord=" + txtTitle.Text + "&IsVisible=" + GetIsVisible() + "&page={0}";
        smartPager.Visible = RowCount > PageSize;

    }
    protected void grv_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (GridViewSortDirection.Equals("ASC"))
            GridViewSortDirection = "DESC";
        else
            GridViewSortDirection = "ASC";
        string strSort = e.SortExpression + " " + GridViewSortDirection;
        BindData(strSort);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {

        Response.Redirect("/Admin/Module/Video/VideoList.aspx?VideoCategoryId=" + ddlVideoCategory.SelectedValue + "&KeyWord=" + txtTitle.Text + "&IsVisible=" + GetIsVisible());
    }
    protected void grv_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("DeleteItem"))
        {
            HiddenField hdfId = (HiddenField)grv.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("hdfId");
            try
            {
                TblVideo obj = TblVideoRepository.CreateInstant().SelectOne(new Guid(hdfId.Value));
                System.IO.File.Delete(Server.MapPath(obj.ImageUrl));
            }
            catch
            {
                Trace.Write("Loai xoa hinh");
            }


            TblVideoRepository.CreateInstant().Delete(new Guid(hdfId.Value));
            Response.Redirect(Request.RawUrl);
        }
    }
    private int GetIsVisible()
    {
        int IsVisible = 0;
        if (cbIsVisible.Checked && !cbIsVisible1.Checked)
            IsVisible = 1;
        else if (!cbIsVisible.Checked && cbIsVisible1.Checked)
            IsVisible = -1;
        return IsVisible;
    }
    private bool? GetIsVisibleBool()
    {
        bool? IsVisible = null;
        if (Request["IsVisible"] != null)
            switch (Request["IsVisible"])
            {
                case "1":
                    IsVisible = true;
                    break;
                case "-1":
                    IsVisible = false;
                    break;
                default:
                    IsVisible = null;
                    break;
            }
        return IsVisible;
    }
}
